using System;
using FrameWork.Inventory;
using UnityEngine;

/// <summary>
/// 保存玩家数据信息
/// </summary>
public class Player : Singleton<Player>
{
    #region Variables
    // 玩家等级配置
    // private PlayerLvSO lv;

    /******* 读取存档数据初始化信息 ******/
    private float playerCurrentLv;                       // 玩家当前等级 
    private float playerCurrentCoins;                    // 玩家当前金币数量
    private float playerCurrentExp;                      // 玩家当前经验值
    private float playerMaxHealth;                       // 玩家最大生命值
    private float playerMaxExp;                          // 玩家升级所需最大经验值

    /************ 临时变量 *************/
    private float currentHealth;                         // 玩家当前血量 
    private float playerMoveEffect = 1;                // 玩家移动影响系数
    
    /************ 玩家状态 *************/
    public bool IsHit{get;set;} = false;               // 玩家是否受伤
    public bool IsDeath { get; private set; } = false; // 玩家是否死亡

    /**********    事件      **********/
    private event Action<Player> updateInfo;           // 玩家数据改变后，更新UI面板
    
    /*********** 玩家工具栏 *************/
    private PlayerToolkit currentTool;                 // 当前所选工具
    private PlayerKit currentKit;                      // 上次使用工具
    private string currentItemName;                    // 当前所选择物品

    #endregion

    #region InitData

    // public void SetLv(PlayerLvSO lv)
    // {
    //     this.lv = lv;
    // }

    public void InitInfo()
    {
        // 存档数据初始化
        // playerCurrentLv = 1;
        // playerCurrentCoins = 100;
        // playerCurrentExp = 0;
        // currentHealth = 100;
        // playerMaxHealth = 100;
        // playerMaxExp = 100;

        // 临时变量初始化
        playerMoveEffect = 1f;
        // currentHealth = playerMaxHealth;

        // 数据初始化后通知UI面板进行更新
        UpdateInfo();
    }

    /// <summary>
    /// 存档数据初始化
    /// 读取存档加载完成后调用该文件
    /// </summary>
    /// <param name="playerData"></param>
    public void LoadSavaData(PlayerDataSO playerData)
    {
        playerCurrentLv = playerData.PlayerLv;
        playerCurrentCoins = playerData.PlayerCoins;
        playerCurrentExp = playerData.PlayerCurrentExp;
        playerMaxHealth = playerData.PlayerMaxHealth;
        playerMaxExp = playerData.PlayerMaxExp;
        currentHealth = playerMaxHealth;
        // 读档完成后通知UI面板更新
        UpdateInfo();
    }

    #endregion

    #region PlayerInformation

    /// <summary>
    /// 获取等级
    /// </summary>
    /// <returns></returns>
    public float GetPlayerLv()
    {
        return playerCurrentLv;
    }

    /// <summary>
    /// 获取玩家当前伤害
    /// </summary>
    /// <returns></returns>
    public float GetPlayerDamage()
    {
        return 10;
    }

    /// <summary>
    /// 获取金币数量
    /// </summary>
    /// <returns></returns>
    public float GetPlayerCoins()
    {
        return playerCurrentCoins;
    }

    /// <summary>
    /// 获取当前金币状态
    /// 购买物品调用
    /// 直接传入金币数量
    /// </summary>
    /// <param name="playerCoins"></param>
    /// <returns></returns>
    public bool GetCurrentConisState(float playerCoins)
    {
        if (playerCurrentCoins - playerCoins < 0)
            return false;
        return true;
    }

    /// <summary>
    /// 更新玩家硬币数量
    /// 增+ 减-
    /// </summary>
    /// <param name="playerCoins"></param>
    /// <returns></returns>
    public void UpdatePlayerCoins(float playerCoins)
    {
        playerCurrentCoins += playerCoins;
        UpdateInfo();
    }

    /// <summary>
    /// 获取当前血量
    /// UI面板使用
    /// 当前血量 / 最大血量
    /// </summary>
    /// <returns></returns>
    public float GetPlayerHealth()
    {
        // if (lv != null)
            // return currentHealth / playerMaxHealth;
            
        // Debug.LogError("LV等级配置文件丢失！");
        // return 0;

        Debug.Log("当前血量比例" + currentHealth / playerMaxHealth);
        Debug.Log("当前血量" + currentHealth);
        Debug.Log("当前血量" + playerMaxHealth);
        return (currentHealth / playerMaxHealth);
    }
    
    /// <summary>
    /// 更新玩家血量
    /// 更新玩家死亡状态
    /// 加血正
    /// 扣血负
    /// </summary>
    /// <param name="value"></param>
    public void UpdatePlayerHealth(float value)
    {
        Debug.Log("血量变化" + value);
        currentHealth += value;
        if (currentHealth > playerMaxHealth)
        {
            currentHealth = playerMaxHealth;
            IsDeath = false;
        }
        if (currentHealth <= 0)
        {
            currentHealth = 0;
            IsDeath = true;
        }
        
        // 玩家血量变化后通知UI面板更新
        UpdateInfo();
    }

    /// <summary>
    /// 获取当前等级经验
    /// UI面板使用
    /// 当前经验 / 升级所需经验
    /// </summary>
    /// <returns></returns>
    public float GetPlayerExp()
    {
        // if (lv != null)
        //     return playerCurrentExp / playerMaxExp;
        //
        // Debug.LogError("LV等级配置文件丢失！");
        // return 0;

        return playerCurrentExp / playerMaxExp;
    }
    
    /// <summary>
    /// 更新玩家当前经验
    /// </summary>
    /// <param name="value"></param>
    public void UpdatePlayerExp(float value)
    {
        playerCurrentExp += value;
        if (playerCurrentExp >= playerMaxExp)
        {
            // 减去升级所需要的经验
            playerCurrentExp -= playerMaxExp;
            // 等级加一
            playerCurrentLv += 1;
        }
        
        // 玩家经验变化后通知UI面板更新
        UpdateInfo();
    }
    #endregion

    #region PlayerMove

    /// <summary>
    /// 设置玩家移动系数
    /// </summary>
    /// <param name="value"></param>
    public void SetPlayerMoveEffect(float value)
    {
        playerMoveEffect = value;
    }

    /// <summary>
    /// 获取玩家移动系数
    /// </summary>
    /// <returns></returns>
    public float GetPlayerMoveEffect()
    {
        return playerMoveEffect;
    }
    

    #endregion
    

    #region PlayerKit
    /// <summary>
    /// 设置玩家当前所选工具
    /// </summary>
    public void SetCurrentKit(PlayerKit kit, PlayerToolkit tool)
    {
        currentKit = kit;
        currentTool = tool;
        // Debug.Log("玩家当前选择是"+currentKit+"选择工具" + currentTool);
    }
    
    /// <summary>
    /// 设置玩家当前所选择的物品
    /// </summary>
    /// <param name="kit"></param>
    /// <param name="inventoryItem"></param>
    public void SetCurrentKit(PlayerKit kit, string itemName)
    {
        currentKit = kit;
        if (currentItemName == null || currentItemName != itemName)
            currentItemName = itemName;
        // Debug.Log("玩家当前选择是"+currentKit+"选择工具" + currentInventoryItem);
    }
    
    /// <summary>
    /// 获取当前栏内状态
    /// 工具/物品
    /// </summary>
    /// <returns></returns>
    public PlayerKit GetCurrentKit()
    {
        return currentKit;
    }

    /// <summary>
    /// 获取当前工具
    /// </summary>
    /// <returns></returns>
    public PlayerToolkit GetCurrentToolKit()
    {
        return currentTool;
    }
    
    /// <summary>
    /// 获取当前所选择的物品
    /// </summary>
    /// <returns></returns>
    public string GetCurrentInventoryItemName()
    {
        return currentItemName;
    }
    #endregion

    #region EventAction

    /// <summary>
    /// 通知UI面板进行数据更新
    /// </summary>
    public void UpdateInfo()
    {
        updateInfo?.Invoke(this);
    }

    /// <summary>
    /// 添加事件
    /// </summary>
    /// <param name="action"></param>
    public void AddListener(Action<Player> action)
    {
        updateInfo += action;
    }
    
    /// <summary>
    /// 移除事件
    /// </summary>
    /// <param name="action"></param>
    public void RemoveListener(Action<Player> action)
    {
        updateInfo -= action;
    }

    #endregion
    
}